// pages/SCM/store/goodsAdd/goodsAdd.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  data: {
    hideFlag:true,
    categories:[],
    imgArray: [],
    goodsItem:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGoodsCategoryList();
    if (options.id){
      this.data.id = parseInt(options.id);
      this.goodsItem();
    }
    this.setData({
      scrollHeight: app.getScrollHeight(0)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    //选择医院完后，获取医院下人员。在这里处理。

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  getGoodsCategoryList: function () {
    let that = this;
    lbbService.getGoodsCategoryList({}, function (res) {
      that.setData({
        categories: res.data
      });
    })
  },
  goodsItem:function(){
    let that=this;
    lbbService.goodsItem({id:that.data.id},function(res){
      that.setData({ 
        goodsItem:res.data,
        categoryId: res.data.categoryId,
        categoryName: res.data.categoryName
      });
    });
  },
  chooseCategory: function (e) {
    console.log(e)
    let that = this
    let category=e.currentTarget.dataset.category;
    that.setData({
      categoryId: category.id,
      categoryName: category.name
    });
  },

  showModel: function () {
    this.setData({
      hideFlag: false
    })
  },

  hideModel: function () {
    this.setData({
      hideFlag: true
    })
  },
 
  formSubmit: function(e){
    let that = this
    console.log(e)
    let formData = e.detail.value
    that.setData({
      formData: formData
    })
    if (!formData.name) {
      wx.showToast({
        title: '填写物品名称',
        image: '/images/gantan.png'
      })
      return
    }
    if (!formData.categoryId){
      wx.showToast({
        title: '请选择物品类别',
        image: '/images/gantan.png'
      })
      return
    }
    
    let imgArray = that.data.imgArray
    wx.showLoading({
      title: '正在上传图片',
    })
    util.upLoadImg(app.globalData.header, imgArray, 0, [], that.successBack)
  },

  successBack: function (imgIdArray){
    let that = this
    wx.hideLoading()
    that.setData({
      imgIdArray
    })
    let formData=that.data.formData;
    let imgIds=that.data.imgIdArray.join();
    if(imgIds){
      formData.imgIds=imgIds;
    }
    lbbService.saveGoods(formData,function(res){
      wx.showToast({
        title: '提交成功',
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    })
  },


  
  
  addImg: function (e) {
    let that = this;
    wx.chooseImage({
      count: 12 - that.data.imgArray,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        for (let i = 0; i < res.tempFilePaths.length; i++) {
          that.data.imgArray.push(res.tempFilePaths[i])
        }
        that.setData({
          imgArray: that.data.imgArray
        })
        console.log(that.data.imgArray)
      }
    })
  },
  deleteImg: function (e) {
    let that = this
    let deleteIndex = e.currentTarget.dataset.index
    that.data.imgArray.splice(deleteIndex, 1)
    that.setData({
      imgArray: that.data.imgArray
    })
  },

  /** 
   * 预览图片
   */
  previewImage: function (e) {
    var urls = e.target.dataset.src;
    wx.previewImage({
      urls: urls // 需要预览的图片http链接列表
    })
  },
 
  
})